package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.advice.CostomReturnException;
import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.ApartmentInfoMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private ProvinceInfoService provinceInfoService;
    @Autowired
    private CityInfoService cityInfoService;
    @Autowired
    private DistrictInfoService districtInfoService;


    @Autowired
    private RoomInfoService roomInfoService;


    @Override
    public ApartmentDetailVo customFindById(Long id) {
        return apartmentInfoMapper.customQueryById(id);
    }

    @Override
    public void findPageCurrentList(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        //
         apartmentInfoMapper.queryPageCurrentList(page,queryVo);
    }

    @Override
    public void customSaveOrUpdat(ApartmentSubmitVo apartmentSubmitVo) {

        // 判断动作条件，count>0修改 ，其他均为添加
        boolean b = apartmentSubmitVo.getId() == null;

        //地区回写
        ProvinceInfo provinceInfo = provinceInfoService.getById(apartmentSubmitVo.getProvinceId());
        CityInfo cityInfo = cityInfoService.getById(apartmentSubmitVo.getCityId());
        DistrictInfo districtInfo = districtInfoService.getById(apartmentSubmitVo.getDistrictId());
        apartmentSubmitVo.setProvinceName(provinceInfo.getName());
        apartmentSubmitVo.setCityName(cityInfo.getName());
        apartmentSubmitVo.setDistrictName(districtInfo.getName());


        //添加或修改主体表,接口已定义操作表，会自动从vo中提出非主体表字段
        saveOrUpdate(apartmentSubmitVo);

        //根据动作，确认是否执行引用表数据的删除动作
        if (!b){
            customRemoveQuote(apartmentSubmitVo.getId());
        }

        //添加引用表数据

        //配套表 ApartmentFacility
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        //判断是否有传值
        if (!CollectionUtils.isEmpty(facilityInfoIds)){
            //创建配套类集合，装所有传来的配套信息
            List<ApartmentFacility> apartmentFacilityList = new ArrayList<>(facilityInfoIds.size());
            //遍历传来的配套id
            for (Long facilityInfoId : facilityInfoIds) {
                //创造者模式创建配套类对象并属性赋值
                ApartmentFacility apartmentFacility = ApartmentFacility.builder().apartmentId(apartmentSubmitVo.getId())
                        .facilityId(facilityInfoId).build();
                //装入list中
                apartmentFacilityList.add(apartmentFacility);
            }

            //添加数据
            apartmentFacilityService.saveBatch(apartmentFacilityList);
        }

        //标签表 ApartmentLabel

        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if (!CollectionUtils.isEmpty(labelIds)){
            //创建标签类集合，装所有传来的标签信息
            List<ApartmentLabel> apartmentLabelList = new ArrayList<>(labelIds.size());
            //遍历传来的标签id
            for (Long labelId : labelIds) {
                //创造者模式创建标签类对象并属性赋值
                ApartmentLabel apartmentLabel = ApartmentLabel.builder().apartmentId(apartmentSubmitVo.getId())
                        .labelId(labelId).build();
                //装入list中
                apartmentLabelList.add(apartmentLabel);
            }

            //添加数据
            apartmentLabelService.saveBatch(apartmentLabelList);
        }
        //杂费表 ApartmentFeeValue
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if (!CollectionUtils.isEmpty(feeValueIds)) {
            //创建杂费类集合，装所有传来的杂费信息
            List<ApartmentFeeValue> apartmentFeeValueList = new ArrayList<>();
            //遍历传来的杂费id
            for (Long feeValueId : feeValueIds) {
                //创造者模式创建杂费类对象并属性赋值
                ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder().apartmentId(apartmentSubmitVo.getId())
                        .feeValueId(feeValueId).build();
                //装入list中
                apartmentFeeValueList.add(apartmentFeeValue);
            }
            //添加数据
            apartmentFeeValueService.saveBatch(apartmentFeeValueList);
        }

        //图片表 GraphInfo
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)) {
            List<GraphInfo> graphInfoList = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                //创建图片对象，对象赋值
                GraphInfo graphInfo= new GraphInfo();
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setItemId(apartmentSubmitVo.getId());
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());

                //list装对象
                graphInfoList.add(graphInfo);
            }
            //添加对象
            graphInfoService.saveBatch(graphInfoList);
        }

    }

    @Override
    public void customRemoveById(Long id) {
        //判断房间表中是否有房间属于当前公寓
        LambdaQueryWrapper<RoomInfo> roomInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomInfoLambdaQueryWrapper.eq(RoomInfo::getApartmentId,id);
        long count = roomInfoService.count(roomInfoLambdaQueryWrapper);
        //有抛异常，删除失败
        if (count>0){
            throw new CostomReturnException(ResultCodeEnum.DELETE_ERROR);
        }
        //没有正常删除
        //先删除非核心引用表
        customRemoveQuote(id);
        //删除主表
        removeById(id);

    }

    private void customRemoveQuote(Long id) {
        //删除公寓配套信息 ApartmentFacility
        LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper =
                new LambdaQueryWrapper<>();
        apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId, id);
        apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);

        //删除公寓标签信息 ApartmentLabel
        LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper =
                new LambdaQueryWrapper<>();
        apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId, id);
        apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);

        //删除公寓杂费信息 ApartmentFeeValue
        LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper =
                new LambdaQueryWrapper<>();
        apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId, id);
        apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);

        //删除公寓图片信息 GraphInfo
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper =
                new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, id)
                        .eq(GraphInfo::getItemType, ItemType.APARTMENT); //图片数据表要指定归属类型：房间、公寓
        graphInfoService.remove(graphInfoLambdaQueryWrapper);
    }
}




